﻿
/** 
 * Twilight Common JS for JQuery 
 * @Author：feitianxinhong http://xuzhihong1987.blog.163.com/  
 * @Version：2.0.0
 * @CreateDate：2012-10-16
 * @Require: 
 * @Example: Visit http://blog.163.com/twi_fly  
 * @Copyright ©2010-2015 Tyingsoft Software Development Studio 之 Twilight/Twifly开源系列. 转载请保留本行注明. 【页脚单行】    
 * @Description：Twilight for Bootstrap3.0 响应式布局
 * ------------------------------------------------------------------
 *  编号    版本号      作者              修改日期        修改内容
 * ------------------------------------------------------------------
 *   1      1.0.0       feitianxinhong    2012-10-16      根据之前的Twi使用Extjs框架改成使用jquery的简化版      
 *   2      1.0.1       feitianxinhong    2015-01-17      不自动加载加载Twi.Msg
 * ------------------------------------------------------------------
 */
(function () {
    var global = this;

    if (typeof Twi === 'undefined') {
        global.Twi = {};
    }
    Twi.global = global;

})();

/**
* PageHandler路径
*/
Twi.Url = "/PageHandler.ashx";

/**
*config:$.ajax(config)的参数
*config.data:参数配置
*config.success:（做了些改进）  twiReturn.success == true时执行的函数
*/
Twi.Ajax = function (config) {
    var ajaxConfig = $.extend({
        url: Twi.Url,
        dataType: 'JSON',
        type: 'POST',
        errorMsgTarget: 'body',  //错误信息显示的位置，默认：body 。 ID需带井号，如：#id
        emptyMsg:false           //显示错误信息前是否清空内容
    }, config);

    ajaxConfig.success = function (twiReturn, textStatus, jqXHR) {

        if (twiReturn.success) {
            if (typeof config.success == "function") config.success(twiReturn);
        }
        else {
            if (Twi.Msg) {
                Twi.Msg.Error(twiReturn.message);
            }
            else {
                if (ajaxConfig.emptyMsg) { $(ajaxConfig.errorMsgTarget).emptyMsg();}
                $(ajaxConfig.errorMsgTarget).append('<div class="alert alert-warning alert-dismissable"><button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button><strong>温馨提示：</strong>' + twiReturn.errors + '</div>');
            }
            if (twiReturn.knowError == "102") {
                location.href = "/Login/Index";
            }
        }
    }
    $.ajax(ajaxConfig);
}
/**
* Grid一页显示的记录条数
*/
Twi.PageSize = 15;

/**
*开发人员才能维护基础数据，发布的时候，或是给测试的时候，这个应设为false
*/
Twi.IsDeveloper = false;
Twi.NotDeveloperError = "系统基础支撑数据，只能由开发人员维护，</br>否则系误操作可能会引起系统对应功能无法操作！";
/**
*获取地址栏参数
*/
Twi.GetUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

//获取URL传值
function queryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
};

//var startDate = (new Date()).format('yyyy-MM-dd');
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
 (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
 RegExp.$1.length == 1 ? o[k] :
 ("00" + o[k]).substr(("" + o[k]).length));
    return format;
};


//Cookies操作
var Cookies = {};
Cookies.set = function (name, value) { var argv = arguments; var argc = arguments.length; var expires = (argc > 2) ? argv[2] : null; var path = (argc > 3) ? argv[3] : '/'; var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : false; document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "") };
Cookies.get = function (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; var j = 0; while (i < clen) { j = i + alen; if (document.cookie.substring(i, j) == arg) return Cookies.getCookieVal(j); i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break } return null };
Cookies.clear = function (name) { if (Cookies.get(name)) { document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" } };
Cookies.getCookieVal = function (offset) { var endstr = document.cookie.indexOf(";", offset); if (endstr == -1) { endstr = document.cookie.length } return unescape(document.cookie.substring(offset, endstr)) };

/*当前登录用户*/
Twi.CurrentUser = {
    FID: Cookies.get('Twi_User_FID'),
    FREALNAME: Cookies.get('Twi_User_FREALNAME'),
    FNAME: Cookies.get('Twi_User_FNAME'),
    PHONENUMBER: Cookies.get('Twi_User_PHONENUMBER'),
    PARKCODE: Cookies.get('Twi_User_PARKCODE'),
    STORAGECODE: Cookies.get('Twi_User_STORAGECODE')
};


function ClearUserCookies() {
    Cookies.clear("Twi_User_FID");
    Cookies.clear("Twi_User_FREALNAME");
    // Cookies.clear("Twi_User_FNAME");
    Twi.CurrentUser.FID = "";
    Twi.CurrentUser.FREALNAME = "";
    Twi.CurrentUser.FNAME = "";
    Twi.CurrentUser.PHONENUMBER = "";
    Twi.CurrentUser.PARKCODE = "";
    Twi.CurrentUser.STORAGECODE = "";

}

///获取当前时间
function CurentTime(isTime) {
    var now = new Date();
    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日
    var hh = now.getHours();            //时
    var mm = now.getMinutes();          //分
    var ss = now.getSeconds();          //秒

    var clock = year + "-";
    if (isTime) {
        if (month < 10)
            clock += "0";
        clock += month + "-";
        if (day < 10)
            clock += "0";
        clock += day + " ";
        if (hh < 10)
            clock += "0";
        clock += hh + ":";
        if (mm < 10)
            clock += '0';
        clock += mm + ":";
        if (ss < 10)
            clock += '0';
        clock += ss;
    }
    else {
        if (month < 10)
            clock += "0";
        clock += month + "-";
        if (day < 10)
            clock += "0";
        clock += day;
    }
    return (clock);
}

///获取当前时间
var DateTime = {
    Curent: {
        now: new Date(),
        data: function () {
            var curent = DateTime.Curent
            var year = curent.now.getFullYear();
            var month = curent.now.getMonth() + 1;
            var day = curent.now.getDate();

            var clock = year + "-";
            if (month < 10)
                clock += "0";
            clock += month + "-";
            if (day < 10)
                clock += "0";
            clock += day;
            return clock;
        }
    },
    CurentDate: function () {
        return DateTime.Curent.data();
    },
    CurentTime: function () {
        var curent = DateTime.Curent
        var hh = curent.now.getHours();
        var mm = curent.now.getMinutes();
        var ss = curent.now.getSeconds();

        var clock = "";
        if (hh < 10)
            clock += "0";
        clock += hh + ":";
        if (mm < 10)
            clock += '0';
        clock += mm + ":";
        if (ss < 10)
            clock += '0';
        clock += ss;
        return clock;
    }
    , CurrentDateTime: function () {
        return DateTime.CurentDate() + " " + DateTime.CurentTime();
    }
};




